﻿using gregn6Lib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XYX.Print.Utils.GridReport
{
    public class GridReport
    {
        #region 成员变量
        private GridppReport Report = null;
        /// <summary>
        /// 打印模板
        /// </summary>
        public string TemplateFile { get; set; }
        #endregion

        #region 构造方法
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="templateFile">打印模板文件路径</param>
        public GridReport()
        {
            try
            {
                Report = new GridppReport();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 打印操作
        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="dicInfo">打印模板参数与值的键值对</param>
        /// <param name="showPrintDialog">是否显示打印对话框</param>
        public void Print(Dictionary<string, string> dicInfo, bool showPrintDialog = false)
        {
            if (Report == null)
            {
                throw new Exception("打印软件尚未初始化");
            }

            if (dicInfo == null || dicInfo.Count <= 0)
            {
                throw new Exception("打印参数尚未初始化，请设置打印参数");
            }
            if (string.IsNullOrEmpty(this.TemplateFile))
            {
                throw new Exception("打印模板尚未初始化，请绑定有效的打印模板");
            }
            try
            {
                Report.LoadFromFile(this.TemplateFile);
                foreach (string item in dicInfo.Keys)
                {
                    if (Report.ParameterByName(item) != null)
                    {
                        Report.ParameterByName(item).AsString = dicInfo[item];
                    }
                }
                Report.Print(showPrintDialog);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 预览操作
        /// <summary>
        /// 预览操作
        /// </summary>
        /// <param name="dicInfo"></param>
        /// <param name="showPrintDialog"></param>
        public void PrintPreview(Dictionary<string, string> dicInfo, bool showPrintDialog = false)
        {
            if (Report == null)
            {
                throw new Exception("打印软件尚未初始化");
            }

            if (dicInfo == null || dicInfo.Count <= 0)
            {
                throw new Exception("打印参数尚未初始化，请设置打印参数");
            }
            if (string.IsNullOrEmpty(this.TemplateFile))
            {
                throw new Exception("打印模板尚未初始化，请绑定有效的打印模板");
            }
            try
            {
                Report.LoadFromFile(this.TemplateFile);
                foreach (string item in dicInfo.Keys)
                {
                    if (Report.ParameterByName(item) != null)
                    {
                        Report.ParameterByName(item).AsString = dicInfo[item];
                    }
                }
                Report.PrintPreview(showPrintDialog);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

    }
}
